home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / WLIST.ARJ / LINKLIST.C next >
Text File  |  1992-05-16  |  4KB  |  181 lines

  1. #include <LinkList.h>
  2. #pragma hdrstop
  3.  
  4. // written by Paul Wheaton
  5.  
  6. LinkedList::LinkedList()
  7.   {
  8.     RootRec=NULL;
  9.     CurRec=NULL;
  10.     NumRecs=0;
  11.     CurRecNum=0;
  12.   }
  13.  
  14. LinkedList::~LinkedList()
  15.   {
  16.     For(CurRecNum,NumRecs)
  17.       {
  18.         LinkedListElement *Temp=CurRec->NextRec;
  19.         delete CurRec;
  20.         CurRec=Temp;
  21.       }
  22.   }
  23.  
  24. Bool LinkedList::Add(void *NewRec)
  25.   {
  26.     LinkedListElement *LLE= new LinkedListElement;
  27.     if (LLE==NULL) return False;
  28.     LLE->Rec=NewRec;
  29.     if (NumRecs)
  30.       {
  31.         LLE->PrevRec=CurRec;
  32.         LLE->NextRec=CurRec->NextRec;
  33.         CurRec->NextRec->PrevRec=LLE;
  34.         CurRec->NextRec=LLE;
  35.         CurRecNum++;
  36.       }
  37.     else
  38.       {
  39.         RootRec=LLE;
  40.         LLE->NextRec=LLE;
  41.         LLE->PrevRec=LLE;
  42.       }
  43.     CurRec=LLE;
  44.     NumRecs++;
  45.     return True;
  46.   }
  47.  
  48. Bool LinkedList::Append(void *NewRec)
  49.   {
  50.     if (Size())
  51.       {
  52.         Root();
  53.         Prev();
  54.       }
  55.     return Add(NewRec);
  56.   }
  57.  
  58. void* LinkedList::operator[](Long Index)
  59.   {
  60.     if (NumRecs==0) return NULL;
  61.     if (NumRecs==1) return(CurRec->Rec);
  62.     Index%=NumRecs;
  63.     if (Index==CurRecNum+1) return(Next());
  64.     if (Index==CurRecNum) return(CurRec->Rec);
  65.     if (Index==0) return(Root());
  66.     if (Index+1==CurRecNum) return(Prev());
  67.     Root();
  68.     Long I=NumRecs/2;
  69.     if (Index>I)
  70.       {
  71.         Long StopVal=NumRecs-Index;
  72.         For(I,StopVal) Prev();
  73.       }
  74.     else For(I,Index) Next(); // search forwards
  75.     return (CurRec->Rec);
  76.   }
  77.  
  78. void LinkedList::DelCur()
  79.   {
  80.     if (NumRecs>1)
  81.       {
  82.         if (RootRec==CurRec) RootRec=CurRec->NextRec;
  83.         CurRec->NextRec->PrevRec=CurRec->PrevRec;
  84.         CurRec->PrevRec->NextRec=CurRec->NextRec;
  85.         LinkedListElement *Temp=CurRec->NextRec;
  86.         delete CurRec;
  87.         CurRec=Temp;
  88.         NumRecs--;
  89.         CurRecNum %= NumRecs;
  90.       }
  91.     else if (NumRecs==1)
  92.       {
  93.         NumRecs=0;
  94.         RootRec=NULL;
  95.         delete CurRec;
  96.         CurRec=NULL;
  97.       }
  98.   }
  99.  
  100. Bool LinkedList::Insert(void *NewRec)
  101.   {
  102.     LinkedListElement *LLE= new LinkedListElement;
  103.     if (LLE==NULL) return False;
  104.     LLE->Rec=NewRec;
  105.     if (NumRecs)
  106.       {
  107.         LLE->PrevRec=CurRec->PrevRec;
  108.         LLE->NextRec=CurRec;
  109.         CurRec->PrevRec->NextRec=LLE;
  110.         CurRec->PrevRec=LLE;
  111.         if (CurRecNum==0) RootRec=LLE;  // insert before the root
  112.       }
  113.     else
  114.       {
  115.         RootRec=LLE;
  116.         LLE->NextRec=LLE;
  117.         LLE->PrevRec=LLE;
  118.       }
  119.     CurRec=LLE;
  120.     NumRecs++;
  121.     return True;
  122.   }
  123.  
  124. void* LinkedList::Last()
  125.   {
  126.     if (Size())
  127.       {
  128.         Root();
  129.         return Prev();
  130.       }
  131.     else return NULL;
  132.   }
  133.  
  134. void *LinkedList::Next()
  135.   {
  136.     if (Size())
  137.       {
  138.         CurRecNum=(CurRecNum+1)%NumRecs;
  139.         CurRec=CurRec->NextRec;
  140.         return CurRec->Rec;
  141.       }
  142.     return NULL;
  143.   }
  144.  
  145. void* LinkedList::Pop()
  146.   {
  147.     Root();
  148.     void* P=Prev();
  149.     DelCur();
  150.     Prev();
  151.     return P;
  152.   }
  153.  
  154. void* Queue::Pop()
  155.   {
  156.     void* P=Root();
  157.     DelCur();
  158.     return P;
  159.   }
  160.  
  161. void *LinkedList::Prev()
  162.   {
  163.     if (Size())
  164.       {
  165.         CurRecNum=(CurRecNum+NumRecs-1)%NumRecs;
  166.           //  trying to avoid a 0-1 situation in an unsigned storage area
  167.         CurRec=CurRec->PrevRec;
  168.         return CurRec->Rec;
  169.       }
  170.     return NULL;
  171.   }
  172.  
  173. void *LinkedList::Root()
  174.   {
  175.     if (RootRec==NULL) return NULL;
  176.     CurRecNum=0;
  177.     CurRec=RootRec;
  178.     return CurRec->Rec;
  179.   }
  180.  
  181.